//=============================================================================
#include "CPlayer.h"
#include <iostream>
//=============================================================================
CPlayer::CPlayer():CEntity() {
    Type = 	ENTITY_TYPE_PLAYER;
    cantBombas = 50;
    nombreJugador="Dany";
    cantVidas=200;
    puntos=0;
    X=0;
    Y=0;
}
//-----------------------------------------------------------------------------
void CPlayer::OnLoop() {
	CEntity::OnLoop();
}

//-----------------------------------------------------------------------------
void CPlayer::OnRender(SDL_Surface* Surf_Display) {
	CEntity::OnRender(Surf_Display);
}

//------------------------------------------------------------------------------
void CPlayer::OnCleanup() {
	CEntity::OnCleanup();
}

//------------------------------------------------------------------------------
void CPlayer::OnAnimate() {
	if(SpeedX !=0 || SpeedY!=0) {
		Anim_Control.MaxFrames = this->MaxFrames;
	}else{
		Anim_Control.MaxFrames = 0;
	}

	CEntity::OnAnimate();
}
//------------------------------------------------------------------------------
/*std::vector<t_posicion>CPlayer::ObtenerListaPosiciones(){
}*/
//------------------------------------------------------------------------------
void CPlayer::ObtenerNuevaPosicion(){
    int newX, i=0, newY, posSpawn;
    bool pudeUbicarlo=false,posicionLibre=true,fin=false;
    CMap* Map =CArea::AreaControl.GetMap(1,1);

    while (!pudeUbicarlo){
        posSpawn = rand() % Map->SpawnJList.size() ;
        t_posicion posSpawnJ = Map->SpawnJList.at(posSpawn);
        newX=posSpawnJ.X;
        newY=posSpawnJ.Y;
        i=0;
        fin=false;
        posicionLibre=true;
        while (!fin) {
            if (!this->PosValidObject(ObjectList[i], newX, newY,false)) {
                fin=true;
                posicionLibre=false;
            } else {
                ++i;
                if (i >=ObjectList.size())
                    fin=true;
            }
        }
        if (posicionLibre)
            pudeUbicarlo=true;
    }
    this->X= newX;
    this->Y= newY;
}
//------------------------------------------------------------------------------
bool CPlayer::OnCollision(CObjetoAnimado* Objeto) {
    if ( Objeto->Type == ENTITY_TYPE_ENEMY ||
         Objeto->Type == ENTITY_TYPE_EXPLOSION ||
         Objeto->Type == ENTITY_TYPE_BALA
       ) {
        cantVidas--;
        if (cantVidas<0)
            Inactive=true;
        else
            ObtenerNuevaPosicion();
       }
    return true;
}
//------------------------------------------------------------------------------
bool CPlayer::OnPonerBomba() {
    if(this->cantBombas>0){
        CBomba *nuevaBomba;
        nuevaBomba =new CBomba();
        nuevaBomba->nombreJugador = this->nombreJugador;
        if(nuevaBomba->OnLoad("Bomba.png", 48 ,48 ,3,0) == false ||
           nuevaBomba->OnLoad("Explosion.png", 48 ,48 ,3,1) == false ) {
            return false;
        }
        nuevaBomba->X =this->X;
        nuevaBomba->Y =this->Y+128;
        CObjetoAnimado::ObjectList.push_back(nuevaBomba);
        this->cantBombas--;
    }
}
//=============================================================================
